import request from '@/utils/request'

/**
 * RAG系统接口
 */

// 获取知识库标签列表
export function getRagTagList() {
  return request({
    url: '/api/v1/rag/query_rag_tag_list',
    method: 'get'
  })
}

// 上传文件到知识库
export function uploadFile(data) {
  return request({
    url: '/api/v1/rag/file/upload',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    timeout: 300000, // 5分钟超时
    data,
    validateStatus: function (status) {
      return status >= 200 && status < 300; // 默认的
    }
  })
}

// 分析Git仓库
export function analyzeGitRepository(data) {
  return request({
    url: '/api/v1/rag/analyze_git_repository',
    method: 'post',
    data: data
  })
}

// 清理知识库标签列表
export function clearRagTagList() {
  return request({
    url: '/api/v1/rag/clear_rag_tag_list',
    method: 'post'
  })
}

// 查询知识库内容
export function queryRagContent(ragTag) {
  return request({
    url: '/api/v1/rag/query_rag_content',
    method: 'get',
    params: { ragTag }
  })
}

// 删除知识库标签
export function deleteRagTag(ragTag) {
  // 从store获取用户角色
  const store = require('@/store').default
  const roles = store.getters.roles
  const isAdmin = roles && roles.includes('admin')
  
  return request({
    url: '/api/v1/rag/delete_rag_tag',
    method: 'delete',
    params: { ragTag },
    headers: {
      'X-User-Role': isAdmin ? 'admin' : 'user' // 动态设置用户角色
    }
  })
}

// 删除知识库文档
export function deleteDocument(documentId) {
  // 从store获取用户角色
  const store = require('@/store').default
  const roles = store.getters.roles
  const isAdmin = roles && roles.includes('admin')
  
  return request({
    url: '/api/v1/rag/delete_document',
    method: 'delete',
    params: { documentId },
    headers: {
      'X-User-Role': isAdmin ? 'admin' : 'user' // 动态设置用户角色
    }
  })
}

// 智能搜索知识库
export function searchKnowledgeBase(query, options = {}) {
  return request({
    url: '/api/v1/rag/search',
    method: 'post',
    data: {
      query: query,
      ...options
    }
  })
}

// 获取知识库统计信息
export function getKnowledgeBaseStats() {
  return request({
    url: '/api/v1/rag/stats',
    method: 'get'
  })
}

// 批量查询知识库内容
export function batchQueryRagContent(ragTags) {
  return request({
    url: '/api/v1/rag/batch_query',
    method: 'post',
    data: { ragTags }
  })
} 